package org.minos.edit.controller 
{
	import flash.events.Event;
	import org.aswing.JFrame;
	import org.minos.edit.EditFacate;
	import org.minos.edit.model.AssetsProxy;
	import org.minos.edit.model.vo.ItemVO;
	import org.minos.edit.view.components.CreateScenePane;
	import org.minos.edit.view.components.SceneTable;
	import org.minos.edit.view.TableMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.command.SimpleCommand;
	
	/**
	 * ...
	 * @author Minos
	 */
	public class EditSceneCommand extends SimpleCommand 
	{
		
		public function EditSceneCommand() 
		{
		}
		
		override public function execute(notification:INotification):void 
		{
			super.execute(notification);
			
			var table : SceneTable = facade.retrieveMediator( TableMediator.NAME ).getViewComponent() as SceneTable;
			var dialog : JFrame = new JFrame( null , "编辑场景" , true );
			var vo:ItemVO = table.getSelectedVO();
			var pane : CreateScenePane = new CreateScenePane();
			dialog.setContentPane( pane );
			
			//set pane by vo
			pane.getLabelTf().setText( vo.label );
			pane.getFrameTf().setText( vo.frame.toString() );
			pane.getChildrenTf()["setText"]( vo.children );
			pane.getUrlTf().setText(vo.url);
			pane.getScoreTf()["setText"]( vo.score );
			pane.getTooltipTf().setText( vo.tooltip );
			pane.getInfoTf().setText( vo.info );
			pane.getContinueInfoTa().setText( vo.continueInfo );
			pane.getEndInfoTa().setText( vo.endInfo );
			pane.getTypeCombox().setSelectedIndex( vo.type );
			
			//linsteners
			pane.getOkButton().addActionListener( onOk );
			pane.getCancelButton().addActionListener( onCancel );
			pane.getUrlBtn().addActionListener( __onUrlClick );
			pane.getChildrenBtn().addActionListener( __onChildrenClick );
			pane.getScoreBtn().addActionListener( __onScoreClick );
			
			dialog.pack();
			dialog.show();
			
			sendNotification( EditFacate.POSITION , dialog );
			
			// ----- event methods ----- //
			
			function __onUrlClick( e : Event ) : void
			{
				sendNotification( EditFacate.SELECT_FILE , { tf: pane.getUrlTf() } , AssetsProxy.SWF );
			}
			
			function __onChildrenClick( e : Event ) : void
			{
				sendNotification( EditFacate.SELECT_SCENE , { tf: pane.getChildrenTf() , vo: vo , type: "children" } );
			}
			
			function __onScoreClick( e : Event ) : void
			{
				sendNotification( EditFacate.SETTER_SCORE , { tf: pane.getScoreTf() , vo: vo , type: "score" } );
			}
			
			function onOk( e : Event ) : void
			{
				
				vo.label = pane.getLabelTf().getText();
				vo.frame = int( pane.getFrameTf().getText() );
				//
				vo.score = pane.getScoreTf().getText();
				vo.url = pane.getUrlTf().getText();
				vo.tooltip = pane.getTooltipTf().getText();
				vo.info = pane.getInfoTf().getText();
				vo.continueInfo = pane.getContinueInfoTa().getText();
				vo.endInfo = pane.getEndInfoTa().getText();
				vo.type = pane.getTypeCombox().getSelectedIndex();
				if (vo.type != 0 )
					vo.children = [];
				dialog.dispose();
				table.change(vo);
			}
			
			function onCancel( e : Event ) : void
			{
				dialog.dispose();
				sendNotification( EditFacate.SCENE_SELECTION_CHANGED , null );
			}
			
		}
		
	}

}